package com.mayikt;

import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.nio.ByteBuffer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DisruptorTest {

    public static void main(String[] args) {
        // 1.创建可以缓存的线程池，提供发给consumer
        ExecutorService executor = Executors.newCachedThreadPool();
        // 2.创建工厂
        EventFactory<LongEvent> longEventFactory = new LongEventFactory();
        // 3.创建ringbuffer大小
        int ringbuffer = 1024 * 1024;
        // 4.创建Disruptor
        Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(longEventFactory, ringbuffer, executor, ProducerType.MULTI, new YieldingWaitStrategy());
        // 5.连接消费者----注册消费者
        disruptor.handleEventsWith(new LongEventHandler());// 往哪个消费者投递handler信息
        disruptor.handleEventsWith(new LongEventHandler());// 注册第二个消费者
        // 多个消费者 一个生产者，默认重复消费；可以配置分组均摊、允许哪个消费者分配多少个
        // 6.启动
        disruptor.start();
        // 7.创建RingBuffer容器
        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
        // 8.创建生产者
        LongEventProducer producer = new LongEventProducer(ringBuffer);
        // 9.指定缓冲区大小
        ByteBuffer byteBuffer = ByteBuffer.allocate(8);
        for (int i = 0; i < 100; i++) {
            byteBuffer.putLong(0, i);
            producer.onData(byteBuffer);
        }
        // 10.关闭executor和disruptor
        executor.shutdown();
        disruptor.shutdown();
    }
}